﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Teacher
	/// </summary>
	public partial class Teacher
	{
		public Teacher()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string T_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Teacher");
			strSql.Append(" where T_Id='"+T_Id+"' ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.Teacher model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.T_Id != null)
			{
				strSql1.Append("T_Id,");
				strSql2.Append("'"+model.T_Id+"',");
			}
			if (model.T_Password != null)
			{
				strSql1.Append("T_Password,");
				strSql2.Append("'"+model.T_Password+"',");
			}
			if (model.T_Name != null)
			{
				strSql1.Append("T_Name,");
				strSql2.Append("'"+model.T_Name+"',");
			}
			if (model.T_Sex != null)
			{
				strSql1.Append("T_Sex,");
				strSql2.Append("'"+model.T_Sex+"',");
			}
			if (model.T_Department != null)
			{
				strSql1.Append("T_Department,");
				strSql2.Append("'"+model.T_Department+"',");
			}
			if (model.T_Duty != null)
			{
				strSql1.Append("T_Duty,");
				strSql2.Append("'"+model.T_Duty+"',");
			}
			if (model.T_Contact != null)
			{
				strSql1.Append("T_Contact,");
				strSql2.Append("'"+model.T_Contact+"',");
			}
			if (model.T_Email != null)
			{
				strSql1.Append("T_Email,");
				strSql2.Append("'"+model.T_Email+"',");
			}
			strSql.Append("insert into Teacher(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Teacher model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Teacher set ");
			if (model.T_Password != null)
			{
				strSql.Append("T_Password='"+model.T_Password+"',");
			}
			if (model.T_Name != null)
			{
				strSql.Append("T_Name='"+model.T_Name+"',");
			}
			if (model.T_Sex != null)
			{
				strSql.Append("T_Sex='"+model.T_Sex+"',");
			}
			else
			{
				strSql.Append("T_Sex= null ,");
			}
			if (model.T_Department != null)
			{
				strSql.Append("T_Department='"+model.T_Department+"',");
			}
			else
			{
				strSql.Append("T_Department= null ,");
			}
			if (model.T_Duty != null)
			{
				strSql.Append("T_Duty='"+model.T_Duty+"',");
			}
			else
			{
				strSql.Append("T_Duty= null ,");
			}
			if (model.T_Contact != null)
			{
				strSql.Append("T_Contact='"+model.T_Contact+"',");
			}
			else
			{
				strSql.Append("T_Contact= null ,");
			}
			if (model.T_Email != null)
			{
				strSql.Append("T_Email='"+model.T_Email+"',");
			}
			else
			{
				strSql.Append("T_Email= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where T_Id='"+ model.T_Id+"' ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string T_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Teacher ");
			strSql.Append(" where T_Id='"+T_Id+"' " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string T_Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Teacher ");
			strSql.Append(" where T_Id in ("+T_Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Teacher GetModel(string T_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" T_Id,T_Password,T_Name,T_Sex,T_Department,T_Duty,T_Contact,T_Email ");
			strSql.Append(" from Teacher ");
			strSql.Append(" where T_Id='"+T_Id+"' " );
			Maticsoft.Model.Teacher model=new Maticsoft.Model.Teacher();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Teacher DataRowToModel(DataRow row)
		{
			Maticsoft.Model.Teacher model=new Maticsoft.Model.Teacher();
			if (row != null)
			{
				if(row["T_Id"]!=null)
				{
					model.T_Id=row["T_Id"].ToString();
				}
				if(row["T_Password"]!=null)
				{
					model.T_Password=row["T_Password"].ToString();
				}
				if(row["T_Name"]!=null)
				{
					model.T_Name=row["T_Name"].ToString();
				}
				if(row["T_Sex"]!=null)
				{
					model.T_Sex=row["T_Sex"].ToString();
				}
				if(row["T_Department"]!=null)
				{
					model.T_Department=row["T_Department"].ToString();
				}
				if(row["T_Duty"]!=null)
				{
					model.T_Duty=row["T_Duty"].ToString();
				}
				if(row["T_Contact"]!=null)
				{
					model.T_Contact=row["T_Contact"].ToString();
				}
				if(row["T_Email"]!=null)
				{
					model.T_Email=row["T_Email"].ToString();
				}
			}
			return model;
		}
        public DataSet GetReportSubmitInformation(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Student.S_Id,Student.S_Name,Teacher.T_Id,T_Password,T_Name,RS_Id,RS_Title,RS_Formerly,RS_Path,RS_Correcting,RS_Socre,RS_Ask,RS_Ans,RS_Share,RS_Date ");
            strSql.Append("FROM Teacher,ReportSubmit,Student ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select T_Id,T_Password,T_Name,T_Sex,T_Department,T_Duty,T_Contact,T_Email ");
			strSql.Append(" FROM Teacher ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" T_Id,T_Password,T_Name,T_Sex,T_Department,T_Duty,T_Contact,T_Email ");
			strSql.Append(" FROM Teacher ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Teacher ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.T_Id desc");
			}
			strSql.Append(")AS Row, T.*  from Teacher T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

